import CONFIG from './config.js'

class Debug {
    constructor(game) {
        this.game = game;
        this.panel = document.getElementById(CONFIG.debug_panel_id);
        this.panel2 = document.getElementById(CONFIG.debug_panel2_id);
        this.loop_count = 0;
        this.ts = 0;
    }

    show() {
        let game = this.game;
        if (!CONFIG.debug) return;
        let game_info = game.game_info;

        let weapon_list = game_info.weapon_list;
        let bullet_len = 0;
        for (let i = 0; i < weapon_list.length; i++) {
            bullet_len += weapon_list[i].bullet_list.length;
        }

        let debug_panel = this.panel;
        let debug_panel2 = this.panel2;
        this.loop_count++;
        let now = new Date().getTime();
        let info_list = [];
        let info_list2 = [];
        if (now - this.ts >= 1000) {
            Array.apply(null, debug_panel.children).forEach(function (item) {
                item.remove();
            });
            Array.apply(null, debug_panel2.children).forEach(function (item) {
                item.remove();
            });

            let weapon_list = game_info.weapon_list.filter(function (weapon) {
                return weapon.config.type > 1;
            });

            let total_score = 0;

            info_list.push("刷新率：" + this.loop_count,
                "总怪物数：" + game_info.spirit_count,
                "总怪物血量：" + game_info.blood_amount,
                "活动怪物数：" + game_info.spirit_list.length,
                "逃脱怪物数：" + game_info.escape_spirit_count,
                "武器数量：" + weapon_list.length,
                "活跃子弹数量：" + bullet_len);

            weapon_list.forEach(function (weapon, index) {
                total_score += weapon.score
                let fire_duration = weapon.fire_duration / 1000 | 0;
                let arg_score = weapon.score / fire_duration | 0;
                info_list2.push((index + 1) + ". " + weapon.config.label + "射击时长：" + fire_duration + "，: 平均火力=" + arg_score)
            });

            info_list.push("总攻击火力：" + total_score);
            info_list.push("金币：" + game_info.gold);
            info_list.push("关卡：" + this.game.level_manager.level);

            info_list.forEach(function (info) {
                let item = document.createElement("div",);
                item.innerText = info;
                debug_panel.appendChild(item);
            });

            info_list2.forEach(function (info) {
                let item = document.createElement("div",);
                item.innerText = info;
                debug_panel2.appendChild(item);
            });

            this.loop_count = 0;
            this.ts = now;
        }
    }
}

export default Debug;